/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [GobangButton.java]
 * Creation Date        [22-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_game.gobang.version_6;

import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;

/**
 * 五子棋按钮
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class GobangButton {

	private final GobangFrame frame;

	public GobangButton(GobangFrame frame) {
		this.frame = frame;
	}

	/**
	 * 开始游戏
	 * 
	 * @param event
	 */
	public void startGame(MouseEvent event) {
		if (event.getX() > 400 && event.getX() < 470 && event.getY() > 80
				&& event.getY() < 100) {
			int result = JOptionPane.showConfirmDialog(frame, "是否重新开始游戏");
			if (result == 0) {
				restart();
				if (frame.maxTime > 0) {
					initTimeMessage();
				} else {
					frame.blackMessage = "无限制";
					frame.whiteMessage = "无限制";
				}
				frame.repaint();
				JOptionPane.showMessageDialog(frame, "游戏开始");
			}
		}
	}

	/**
	 * 游戏信息
	 * 
	 * @param event
	 */
	public void gameInfo(MouseEvent event) {
		if (event.getX() > 400 && event.getX() < 470 && event.getY() > 180
				&& event.getY() < 200) {
			JOptionPane.showMessageDialog(frame, "五子棋游戏, 黑白双方轮流下棋, 一方连到五子游戏结束");
		}
	}

	/**
	 * "认输"按钮
	 * 
	 * @param event
	 */
	public void giveUp(MouseEvent event) {
		if (event.getX() > 400 && event.getX() < 470 && event.getY() > 280
				&& event.getY() < 300) {
			int result = JOptionPane.showConfirmDialog(frame, "您真的要认输吗?");
			if (result == 0) {
				if (frame.isBlack) {
					frame.message = "黑方认输";
					JOptionPane.showMessageDialog(frame, frame.message);
				} else {
					frame.message = "白方认输";
					JOptionPane.showMessageDialog(frame, frame.message);
				}
				frame.isContinue = false;
				frame.repaint();
			}
		}
	}

	/**
	 * "关于"按钮
	 * 
	 * @param event
	 */
	public void about(MouseEvent event) {
		if (event.getX() > 400 && event.getX() < 470 && event.getY() > 330
				&& event.getY() < 350) {
			JOptionPane.showMessageDialog(frame, "Copy Right by Sean Yang");
		}
	}

	/**
	 * "退出"按钮
	 * 
	 * @param event
	 */
	public void quit(MouseEvent event) {
		if (event.getX() > 400 && event.getX() < 470 && event.getY() > 380
				&& event.getY() < 400) {
			int result = JOptionPane.showConfirmDialog(frame, "您真的想退出吗?");
			if (result == 0) {
				System.exit(0);
			}
		}
	}

	/**
	 * "游戏设置"按钮
	 * 
	 * @param event
	 */
	@SuppressWarnings("deprecation")
	public void gameSetting(MouseEvent event) {
		if (event.getX() > 400 && event.getX() < 470 && event.getY() > 120
				&& event.getY() < 140) {
			String input = JOptionPane.showInputDialog("请输入游戏时间(单位为分钟),0表示无限制");

			try {
				frame.maxTime = (Integer.parseInt(input)) * 60;
				if (frame.maxTime < 0) {
					JOptionPane.showMessageDialog(frame, "请输入正确时间");
				}
				if (frame.maxTime == 0) {
					int result = JOptionPane.showConfirmDialog(frame,
							"设置完成, 是否重新开始游戏");
					if (result == 0) {
						restart();
						frame.blackMessage = "无限制";
						frame.whiteMessage = "无限制";
						if (frame.thread.isAlive()) {
							frame.thread.suspend();
						}
						frame.repaint();
					}
				}
				if (frame.maxTime > 0) {
					int result = JOptionPane.showConfirmDialog(frame,
							"游戏设置完成, 是否重新开始?");
					if (result == 0) {
						restart();
						initTimeMessage();
						frame.thread.resume();
						frame.repaint();
					}
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(frame, "请输入正确时间");
			}
		}
	}

	/**
	 * 重新开始, 初始化所有设置
	 */
	private void restart() {
		frame.allChess = new int[19][19];
		frame.message = "黑方先行";
		frame.isBlack = true;
		frame.isContinue = true;
		// 设置双方时间为初始化时间
		frame.blackTime = frame.maxTime;
		frame.whiteTime = frame.maxTime;
	}

	/**
	 * 初始化时间
	 */
	private void initTimeMessage() {
		frame.blackMessage = frame.maxTime / 3600 + ":"
				+ (frame.maxTime / 60 - frame.maxTime / 3600 * 60) + ":"
				+ (frame.maxTime - frame.maxTime / 60 * 60);
		frame.whiteMessage = frame.maxTime / 3600 + ":"
				+ (frame.maxTime / 60 - frame.maxTime / 3600 * 60) + ":"
				+ (frame.maxTime - frame.maxTime / 60 * 60);
	}
}
